package com.ht.aems.fesmodel;

import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;

/**
 * Created by IntelliJ IDEA.
 * @author Dong Shufeng
 * Date: 2007-1-11
 * Time: 10:53:34
 */
public class DBCharsetConvertor {

    private static Logger log = Logger.getLogger(DBCharsetConvertor.class);

    String dbCharsetName;

    public DBCharsetConvertor(String dbCharsetName) {
        this.dbCharsetName = dbCharsetName;
    }

    public DBCharsetConvertor() {
    }

    public String saveToDb(String toSave) {
        if(dbCharsetName != null && toSave != null)
            try {
                return new String(toSave.getBytes(), dbCharsetName);
            } catch (UnsupportedEncodingException e) {
                log.warn(e);
            }
        return toSave;
    }

    public String getFromDb(String original) {
        if(dbCharsetName != null && original!=null){
            original=original.trim();
            try {
                return new String(original.getBytes(dbCharsetName));
            } catch (UnsupportedEncodingException e) {
                log.warn(e);
            }
        }
        return original;
    }


    public String getDbCharsetName() {
        return dbCharsetName;
    }

    public void setDbCharsetName(String dbCharsetName) {
        this.dbCharsetName = dbCharsetName;
    }
}
